// ******************************************************************
// * Copyright(c)  Steven Lu
// * Description   : Entity_PUR_PURCHASE_ARRIVED_ITEM
// * CreateDate    : 2011/8/16 10:16:41
// * Creater       : CodeSmith
// * LastChangeDate: 2011/8/16 10:16:41
// * LastChanger   : 2011/8/16 10:16:41
// * Version Info  : 1.0 For ESOL
// ******************************************************************

using System;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Suzsoft.Smart.Data;
using ESOL.Common.Entity;

namespace ESOL.Business.Pur
{
/// <summary>
    /// PurPurchaseArrivedItem
    /// </summary>
    public partial class PurPurchaseArrivedItemBs
    {
        #region GetByID
        /// <summary>
        /// Get Entity
        /// </summary>
        /// <param name="objPurPurchaseArrivedItemEntity">Entity</param>
        /// <returns>Entity</returns>
        public static PurPurchaseArrivedItemEntity GetPurPurchaseArrivedItem(PurPurchaseArrivedItemEntity objPurPurchaseArrivedItemEntity)
        {
            return DataAccess.SelectSingle<PurPurchaseArrivedItemEntity>(objPurPurchaseArrivedItemEntity);
        }
        public static PurPurchaseArrivedItemEntity GetPurchaseArrivedItemByID(string itemID)
        {
            if (string.IsNullOrEmpty(itemID))
            {
                return null;
            }
            PurPurchaseArrivedItemEntity entity = new PurPurchaseArrivedItemEntity();
            entity.ArrivedItemId = itemID;
            return DataAccess.SelectSingle<PurPurchaseArrivedItemEntity>(entity);
        }
        public static PurPurchaseArrivedItemEntity GetByPurchaseOrderItemID(string purchaseOrderItemID)
        {
            if (string.IsNullOrEmpty(purchaseOrderItemID))
            {
                return null;
            }
            PurPurchaseArrivedItemEntity entity = new PurPurchaseArrivedItemEntity();
            entity.PurchaseItemId = purchaseOrderItemID;
            List<PurPurchaseArrivedItemEntity> list = DataAccess.Select<PurPurchaseArrivedItemEntity>(entity);
            if (list == null || list.Count == 0)
            {
                return null;
            }
            return list[0];
        }
        public static List<PurPurchaseArrivedItemEntity> GetByPurchaseArrivedID(string purchaseArrivedID)
        {
            if (string.IsNullOrEmpty(purchaseArrivedID))
            {
                return null;
            }
            PurPurchaseArrivedItemEntity entity = new PurPurchaseArrivedItemEntity();
            entity.ArrivedId = purchaseArrivedID;
            return DataAccess.Select<PurPurchaseArrivedItemEntity>(entity);
        }
        public static DataSet GetDataSetByPurchaseArrivedID(string purchaseArrivedID)
        {
            if (string.IsNullOrEmpty(purchaseArrivedID))
            {
                return null;
            }
//            string sql = @"SELECT PAI.*,P.*,POI.QTY,POI.PRICE_NO_TAX, CONVERT(DECIMAL(18,2),POI.QTY*POI.PRICE_NO_TAX) AS TOTAL_AMOUNT, PO.PURCHASE_ORDER_CODE,E.LOCATION_CODE,F.WAREHOUSE_CODE,F.WAREHOUSE_NAME
//                            FROM PUR_PURCHASE_ARRIVED_ITEM PAI
//                            INNER JOIN PUR_PURCHASE_ARRIVED PA ON PA.ARRIVED_ID = PAI.ARRIVED_ID
//							INNER JOIN PUR_PURCHASE_ORDER_ITEM POI ON POI.PURCHASE_ITEM_ID = PAI.PURCHASE_ITEM_ID
//                            INNER JOIN PUR_PURCHASE_ORDER PO ON PO.PURCHASE_ORDER_ID = POI.PURCHASE_ORDER_ID
//							LEFT JOIN MST_PRODUCT P ON P.PRODUCT_ID = POI.PRODUCT_ID
//                            left join INV_STORAGE G on POI.PRODUCT_ID=G.PRODUCT_ID
//                            left join MST_LOCATION E on G.LOCATION_ID=e.LOCATION_ID
//                            left join MST_WAREHOUSE F on f.WAREHOUSE_CODE=e.WAREHOUSE_CODE
//                            WHERE PAI.ARRIVED_ID = '" + purchaseArrivedID + "'";

            //modify by Mars.Chen on 2012-05-11 for show Plan_Order_Code
            string sql = @"SELECT K.PLAN_ORDER_CODE,J.PRODUCT_SPEC_OUT, PAI.*,P.*,POI.QTY,POI.PRICE_NO_TAX, CONVERT(DECIMAL(18,2),POI.QTY*POI.PRICE_NO_TAX) AS TOTAL_AMOUNT, 
                            PO.PURCHASE_ORDER_CODE, V.TOTAL_ARRIVED_QTY
                            FROM PUR_PURCHASE_ARRIVED_ITEM PAI
                            INNER JOIN PUR_PURCHASE_ARRIVED PA ON PA.ARRIVED_ID = PAI.ARRIVED_ID
							INNER JOIN PUR_PURCHASE_ORDER_ITEM POI ON POI.PURCHASE_ITEM_ID = PAI.PURCHASE_ITEM_ID
                            INNER JOIN PUR_PURCHASE_ORDER PO ON PO.PURCHASE_ORDER_ID = POI.PURCHASE_ORDER_ID
							LEFT JOIN MST_PRODUCT P ON P.PRODUCT_ID = POI.PRODUCT_ID
                            LEFT JOIN VIEW_PURCHASE_ARRIVED_QTY V ON V.PURCHASE_ITEM_ID = POI.PURCHASE_ITEM_ID
                            LEFT JOIN DBO.PUR_PURCHASE_ORDER_ITEM_DETAIL H ON H.PURCHASE_ITEM_ID=POI.PURCHASE_ITEM_ID
                            LEFT JOIN DBO.PUR_APPLICATION_ITEM M ON M.APPLICATION_ITEM_ID=H.APPLICATION_ITEM_ID
                            LEFT JOIN DBO.PUR_APPLICATION N ON M.APPLICATION_ID=N.APPLICATION_ID
                            LEFT JOIN DBO.PROD_PLAN_ORDER K ON N.PLAN_ORDER_ID=K.PLAN_ORDER_ID
                            LEFT JOIN DBO.SALES_PROD_PLAN_APPLICATION_ITEM J ON J.APPLICATION_ITEM_ID=K.APPLICATION_ITEM_ID
                            LEFT JOIN DBO.SALES_PROD_PLAN_APPLICATION I ON I.APPLICATION_ID=J.APPLICATION_ID
                                        WHERE PAI.ARRIVED_ID = '" + purchaseArrivedID + "' ";


            return DataAccess.SelectDataSet(sql);
        }
        #endregion

        #region Insert
        /// <summary>
        /// Insert Data
        /// </summary>
        /// <param name="objPurPurchaseArrivedItemEntity">Entity</param>
        /// <returns>true Insert Succeed; false Insert Fail</returns>
        public static bool InsertPurPurchaseArrivedItem(PurPurchaseArrivedItemEntity objPurPurchaseArrivedItemEntity)
        {
            return DataAccess.Insert(objPurPurchaseArrivedItemEntity);
        }

        public static bool InsertPurPurchaseArrivedItem(List<PurPurchaseArrivedItemEntity> objPurPurchaseArrivedItemEntityList)
        {
            return DataAccess.Insert(objPurPurchaseArrivedItemEntityList);
        }
        #endregion

        #region Update
        /// <summary>
        /// Update Data
        /// </summary>
        /// <param name="objPurPurchaseArrivedItemEntity">Entity</param>
        /// <returns>true Update Succeed; false Update Fail</returns>
        public static bool UpdatePurPurchaseArrivedItem(PurPurchaseArrivedItemEntity objPurPurchaseArrivedItemEntity)
        {
            return DataAccess.Update(objPurPurchaseArrivedItemEntity);
        }

        public static bool UpdatePurPurchaseArrivedItem(List<PurPurchaseArrivedItemEntity> objPurPurchaseArrivedItemEntityList)
        {
            return DataAccess.Update<PurPurchaseArrivedItemEntity>(objPurPurchaseArrivedItemEntityList);
        }
        #endregion

        #region Delete
        /// <summary>
        /// Delete Data
        /// </summary>
        /// <param name="objPurPurchaseArrivedItemEntity">Entity</param>
        /// <returns>true Delete Succeed; false Delete Fail</returns>
        public static bool DeletePurPurchaseArrivedItem(PurPurchaseArrivedItemEntity objPurPurchaseArrivedItemEntity)
        {
            return DataAccess.Delete(objPurPurchaseArrivedItemEntity);
        }

        public static bool DeletePurPurchaseArrivedItem(List<PurPurchaseArrivedItemEntity> objPurPurchaseArrivedItemEntityList)
        {
            return DataAccess.Delete<PurPurchaseArrivedItemEntity>(objPurPurchaseArrivedItemEntityList);
        }
        #endregion

        #region GetAll
        /// <summary>
        /// Get All Data
        /// </summary>
        /// <returns>Back All Data<returns>
        public static DataSet GetPurPurchaseArrivedItemAll()
        {
            return DataAccess.SelectAllDataSet<PurPurchaseArrivedItemEntity>();
        }
        /// <summary>
        /// Get All Data List
        /// </summary>
        /// <returns>Back All Data List<returns>
        public static List<PurPurchaseArrivedItemEntity> GetPurPurchaseArrivedItemAllList()
        {
            return DataAccess.SelectAll<PurPurchaseArrivedItemEntity>();
        }
        #endregion

        #region Functions
        public static MstProductEntity GetMstProductByArrivedItemID(string ArrivedItemID)
        {
            if (string.IsNullOrEmpty(ArrivedItemID))
                return null;
            string Sql = @"SELECT MP.* FROM PUR_PURCHASE_ARRIVED_ITEM PPAI
                LEFT JOIN PUR_PURCHASE_ORDER_ITEM PPOI ON PPAI.PURCHASE_ITEM_ID=PPOI.PURCHASE_ITEM_ID
                LEFT JOIN MST_PRODUCT MP ON PPOI.PRODUCT_ID=MP.PRODUCT_ID ";
            Sql += " WHERE PPAI.ARRIVED_ITEM_ID='" + ArrivedItemID + "' ";
            List<MstProductEntity> list=DataAccess.Select<MstProductEntity>(Sql);
            if (list != null && list.Count > 0)
                return list[0];
            else return null;
        }

        public static DataTable GetDtAllByArrivedID(string sArrivedID)
        {
            string sql=@"select * from PUR_PURCHASE_ARRIVED_ITEM ppai
                left join PUR_PURCHASE_ORDER_ITEM ppoi on ppai.PURCHASE_ITEM_ID=ppoi.PURCHASE_ITEM_ID
                left join MST_PRODUCT mp on ppoi.PRODUCT_ID=mp.PRODUCT_ID
                where ppai.ARRIVED_ID='"+sArrivedID+"'";
            return DataAccess.SelectDataSet(sql).Tables[0];
        }
        #endregion
    }
	
	
}	

